Delta Engine Blog

AI, Robotics, multiplatform game development and Strict programming language

AbiTrafficMonitor Tool

A couple of days back I made a blog post about Top 10 tools and included my version of the AppTrafficMonitorLite Tool, which is nice, but does not run very stable (crashes after a while). I couldn't figure out why this happens because the debugger only tells me that some unmanaged gdi stuff crashes (at random memory locations, thats fun).

Instead of analysing and fixing that tool forever while most of the code was not done by me and it is not structurized in a easy way, I decided to write a completly new tool to do the job. It is still based on the great SharpPcap library, which brings WinPcap to .NET. It will not support as many features and I will only implement stuff as I need it ^^

The most important features for me are the autoupdating traffic tray icon and the current and total traffic. More features will come in the detail window later, we are still at version 0.1. The detail window will show all current packages that go through your computer, all the application details and additional information and statistics about all network adapters.

Here is the download link:

Click here to download AbiTrafficMonitor.exe v0.1 (160 KB)


Update 2006-08-08: Fixed the increasing GDI handles bug (still caused crashes after several hours), now it runs REALLY stable :)

Here is a little info about this issue. This was the old code:

    // Set the new icon
    notifyIcon1.Icon = Icon.FromHandle(updatedIconBmp.GetHicon());

Looks ok, but with the help of ProcessExplorer I found out that this lines creates 3 new GDI handles and never releases it. I thought maybe something is wrong with GetHicon generating the bitmap, but that didn't cause it. So it must have something to do with the Icon.FromHandle method. I even tried out implementing the icon with Interop calls, but always as I set the new icon to the notify icon I got 3 new GDI handles, ARG! Finally I thought that the problem may not be in the Icon class, but in the NotifyIcon class and how the new Icon is applied. And thats exactly where the problem is. The Icon set property does not just assign the Icon, it does create a copy and manages the icon itself. So the only thing you have to do after assigning a Icon is to destroy it again (since NotifyIcon keeps its own copy):


    // Create new icon
    Icon newIcon = Icon.FromHandle(updatedIconBmp.GetHicon());

    // Set the new icon
    notifyIcon1.Icon = newIcon;

    // Destroy the icon, since the NotifyIcon keeps its own copy of the icon.
    // Note: This was the GDI memory leak before, is fixed now!

    DestroyIcon(newIcon.Handle);

[...]

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    extern static bool DestroyIcon(IntPtr handle);